//
//  POSCanTransferVC.m
//  NewPro
//
//  Created by 何荧红 on 2019/3/25.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "POSCanTransferVC.h"

#import "POSWithNameCell.h"
#import "POSListCell.h"

#import "PostlistModel.h"
#import "POSStore.h"

@interface POSCanTransferVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, assign) NSInteger VCType;

@property (nonatomic, strong) ViewFoot *foot;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *mutArr;

@property (nonatomic, assign) BOOL isTimesort;
@property (nonatomic, copy) NSString *timesort;

@property (nonatomic, assign) NSIndexPath *Fristindex;
@property (nonatomic, assign) NSIndexPath *Secondindex;


@property (nonatomic, strong) NSMutableDictionary *indexdic;

@end

@implementation POSCanTransferVC

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}
- (NSMutableDictionary *)indexdic{
    if (!_indexdic) {
        _indexdic = [[NSMutableDictionary alloc]init];
    }
    return _indexdic;
}
- (void)setIsTimesort:(BOOL)isTimesort{
    _isTimesort = isTimesort;
    self.timesort = @"expire_start_time";
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
    [self getPOSlist];
}
- (void)initCSS{
    self.timesort = @"";
    self.VCType = 7;
    WS(weakself);
    [self setNavBarWithTitle:@"可回拨终端列表"];
    self.foot = [[ViewFoot alloc]initWithPOSCanTransfer];
    [self.foot setMoneytosureBlock:^{
        [weakself toSureLod];
    }];
    [self.view addSubview:self.foot];
    [self.foot mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(49);
        make.left.right.bottom.equalTo(self.view);
    }];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, iPhoneX_Top + 25, SCREEN_WIDTH, SCREEN_HEIGHT - iPhoneX_Top - 55) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerClass:[POSWithNameCell class] forCellReuseIdentifier:@"POSWithNameCellID"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (self.dataArr.count > 0) {
            [self.dataArr removeAllObjects];
        }
        self.foot.allnumber = 0;
        self.foot.cliknumber = 0;
        [self getPOSlist];
    }];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.equalTo(self.view.mas_left);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.bottom.equalTo(self.foot.mas_top);
    }];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DetailNumberClik:) name:@"POSClikNumber" object:nil];
}
- (void)DetailNumberClik:(NSNotification *)obj{
    NSDictionary *dic = obj.object;
    if ([dic[@"isPiliang"] isEqualToString:@"1"]) {
        [self.indexdic setObject:@"1" forKey:dic.allKeys[0]];
        if (self.Fristindex) {
            NSIndexPath *indexx = [NSIndexPath indexPathForRow:[dic.allValues[0] integerValue] inSection:[dic.allKeys[0] integerValue]];
            if (indexx.section == self.Fristindex.section) {
                if (indexx.row != self.Fristindex.row) {
                    self.Secondindex = indexx;
                    if (self.Fristindex.row > self.Secondindex.row) {
                        if (self.dataArr.count > self.Fristindex.section) {
                            PostlistCanModel *model = self.dataArr[self.Fristindex.section];
                            for (NSInteger i = self.Fristindex.row - 1; i >= self.Secondindex.row + 1; i--) {
                                PostCanDetailModel *Dmodel = model.infoArr[i];
                                Dmodel.isClik = !Dmodel.isClik;
                            }
                        }
                    }else{
                        if (self.dataArr.count > self.Fristindex.section) {
                            PostlistCanModel *model = self.dataArr[self.Fristindex.section];
                            for (NSInteger i = self.Fristindex.row + 1; i <= self.Secondindex.row - 1; i++) {
                                PostCanDetailModel *Dmodel = model.infoArr[i];
                                Dmodel.isClik = !Dmodel.isClik;
                            }
                        }
                    }
                    [self.tableView reloadData];
                    [self ChooseWithindex:indexx];
                    self.Fristindex = nil;
                    self.Secondindex = nil;
                }else{
                    [self ChooseWithindex:indexx];
                }
            }else{
                self.Fristindex = [NSIndexPath indexPathForRow:[dic.allValues[0] integerValue] inSection:[dic.allKeys[0] integerValue]];
                [self ChooseWithindex:indexx];
            }
        }else{
            self.Fristindex = [NSIndexPath indexPathForRow:[dic.allValues[0] integerValue] inSection:[dic.allKeys[0] integerValue]];
            [self ChooseWithindex:self.Fristindex];
        }
    }else if ([dic[@"isPiliang"] isEqualToString:@"0"]){
        [self.indexdic setObject:@"0" forKey:dic.allKeys[0]];
        NSIndexPath *indexx = [NSIndexPath indexPathForRow:[dic.allValues[0] integerValue] inSection:[dic.allKeys[0] integerValue]];
        [self ChooseWithindex:indexx];
    }
}
- (void)ChooseWithindex:(NSIndexPath *)indexPath{
    NSMutableArray *subarr = [[NSMutableArray alloc]init];
    NSInteger allclik = 0;
    if (self.dataArr.count >= indexPath.section + 1) {
        PostlistCanModel *model = self.dataArr[indexPath.section];
        if (model.infoArr.count >= indexPath.row + 1) {
            for (PostCanDetailModel *Dmodel in model.infoArr) {
                if (Dmodel.isClik) {
                    allclik += 1;
                    [subarr addObject:model];
                }
            }
        }
        self.foot.cliknumber = allclik;
    }
    self.mutArr = [subarr valueForKeyPath:@"@distinctUnionOfObjects.self"];
}
- (void)toSureLod{
    NSMutableDictionary *prams = [[NSMutableDictionary alloc]init];
    [prams setObject:[UserDefaultsManager sharedInstance].user_id forKey:@"customerId"];
    if (self.mutArr.count > 0) {
        NSMutableArray * subb = [[NSMutableArray alloc]init];
        for (PostlistCanModel *model in self.dataArr) {
            NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
            if (model.infoArr.count > 0) {
                NSMutableArray *arr = [[NSMutableArray alloc]init];
                for (PostCanDetailModel *Dmodel in model.infoArr) {
                    if (Dmodel.isClik) {
                        [arr addObject:Dmodel.sn];
                    }
                }
                if (arr.count > 0) {
                    [dic setObject:arr forKey:@"snList"];
                    [dic setObject:model.fromId forKey:@"targetId"];
                }
            }
            if (dic.count > 0) {
                [subb addObject:dic];
            }
        }
        [prams setObject:subb forKey:@"snInfo"];
    }
    [[[POSStore alloc]init] GoBackFlowWithPrams:prams Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            [self.tableView.mj_header beginRefreshing];
        }else{
            [self showMBPError:res[@"msg"]];
        }
    } Failure:^(NSError * _Nonnull error) {
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (void)getPOSlist{
    [[[POSStore alloc]init] GetCanBackFlowWithID:[UserDefaultsManager sharedInstance].user_id Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            NSArray *arr = res[@"data"];
            if (arr.count > 0) {
                NSInteger allnumber = 0;
                for (NSDictionary *dic in arr) {
                    PostlistCanModel *model = [PostlistCanModel mj_objectWithKeyValues:dic];
                    model.HeaderISPush = NO;
                    allnumber += [model.count integerValue];
                    [self.dataArr addObject:model];
                }
                self.foot.allnumber = allnumber;
            }else{
                [self showMBPError:@"暂无数据"];
            }
        }else{
            [self showMBPError:res[@"msg"]];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } Failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (void)GetlistData:(PostlistCanModel *)model{
    NSMutableDictionary * prams = [[NSMutableDictionary alloc]init];
    [prams setObject:@"1" forKey:@"current"];
    [prams setObject:model.count forKey:@"size"];
    [prams setObject:[UserDefaultsManager sharedInstance].user_id forKey:@"customerId"];
    if (self.isTimesort) {
        [prams setObject:self.timesort forKey:@"descs"];
    }else{
        [prams setObject:self.timesort forKey:@"ascs"];
    }
    [[[POSStore alloc]init] GetCanBackFlowDetailWithPrams:prams andfromId:model.fromId Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            NSArray *arr = res[@"data"][@"records"];
            if (arr.count > 0) {
                NSMutableArray *subArr = [[NSMutableArray alloc]init];
                for (NSDictionary *dic in arr) {
                    PostCanDetailModel *model = [PostCanDetailModel mj_objectWithKeyValues:dic];
                    [subArr addObject:model];
                }
                model.infoArr = subArr;
            }else{
                [self showMBPError:@"暂无数据"];
            }
        }else{
            [self showMBPError:res[@"msg"]];
        }
        [self.tableView reloadData];
    } Failure:^(NSError * _Nonnull error) {
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataArr.count > 0) {
        PostlistCanModel *model = self.dataArr[indexPath.section];
        if (model.HeaderISPush) {
            CGFloat height = 70;
            if (model.infoArr.count > 0) {
                height = 70 + 20 * model.infoArr.count;
            }
            return height;
        }else{
            return 40;
        }
    }
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *foot = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5)];
    foot.backgroundColor = [UIColor clearColor];
    return foot;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    POSWithNameCell *cell = [tableView dequeueReusableCellWithIdentifier:@"POSWithNameCellID"];
    cell.CellSection = indexPath.section;
    cell.CellType = self.VCType;
    if (self.indexdic.count > 0) {
        for (NSString *str in self.indexdic.allKeys) {
            if ([str integerValue] == indexPath.section) {
                if ([self.indexdic[str] isEqualToString:@"0"]) {
                    cell.ispiliang = NO;
                }else if ([self.indexdic[str] isEqualToString:@"1"]){
                    cell.ispiliang = YES;
                }
            }
        }
    }
    if (self.dataArr.count > 0) {
        PostlistCanModel *model = self.dataArr[indexPath.section];
        cell.contstring = model.realName;
        cell.phonestring = model.phoneNumber;
        cell.numberstr = [model.count integerValue];
        if (model.infoArr.count > 0) {
            cell.dataArr = model.infoArr;
        }
        [cell setTabPushBlock:^(BOOL push) {
            PostlistCanModel *model = self.dataArr[indexPath.section];
            model.HeaderISPush = push;
            if (push) {
                [self GetlistData:model];
            }else{
                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone ];
            }
        }];
        cell.isPush = model.HeaderISPush;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

@end
